/**
 * 
 */
package com.vanstone.imgapp.img.services.impl;

import java.util.ArrayList;
import java.util.List;

import com.vanstone.common.lang.EnumUtils;
import com.vanstone.imgapp.api.img.AbstractArticle;
import com.vanstone.imgapp.api.img.Album;
import com.vanstone.imgapp.api.img.ArticleType;
import com.vanstone.imgapp.api.img.Category;
import com.vanstone.imgapp.api.img.Tag;
import com.vanstone.imgapp.img.dal.object.CmsArticleDO;
import com.vanstone.imgapp.img.dal.object.CmsArticleDetailDO;
import com.vanstone.imgapp.img.dal.object.CmsCategoryDO;
import com.vanstone.imgapp.img.dal.object.CmsTagDO;

/**
 * @author shipeng <br />
 * 
 */
public class BeanUtil {

  public static Tag toTag(CmsTagDO cmsTagDO) {
    Tag tag = new Tag();
    tag.setId(cmsTagDO.getId());
    tag.setTagname(cmsTagDO.getTagname());
    tag.setRelcount(cmsTagDO.getRelcount());
    return tag;
  }

  public static CmsTagDO toCmsTagDO(Tag tag) {
    CmsTagDO cmsTagDO = new CmsTagDO();
    cmsTagDO.setId(tag.getId());
    cmsTagDO.setTagname(tag.getTagname());
    cmsTagDO.setRelcount(tag.getRelcount());
    return cmsTagDO;
  }

  public static Category toCategory(CmsCategoryDO cmsCategoryDO) {
    Category category = new Category();
    category.setId(cmsCategoryDO.getId());
    category.setCategoryName(cmsCategoryDO.getCategoryName());
    category.setCategoryNamePinyin(cmsCategoryDO.getCategoryNamePinyin());
    category.setCoverFile(cmsCategoryDO.getCoverFile());
    category.setContent(cmsCategoryDO.getContent());
    category.setAlbumcount(cmsCategoryDO.getAlbumcount());
    return category;
  }

  public static CmsCategoryDO toCmsCategoryDO(Category category) {
    CmsCategoryDO cmsCategoryDO = new CmsCategoryDO();
    cmsCategoryDO.setId(category.getId());
    cmsCategoryDO.setCategoryName(category.getCategoryName());
    cmsCategoryDO.setCategoryNamePinyin(category.getCategoryNamePinyin());
    cmsCategoryDO.setCoverFile(category.getCoverFile());
    cmsCategoryDO.setContent(category.getContent());
    cmsCategoryDO.setAlbumcount(category.getAlbumcount());
    return cmsCategoryDO;
  }

  public static AbstractArticle toArticle(CmsArticleDO cmsArticleDO, CmsArticleDetailDO cmsArticleDetailDO, List<Tag> tags, Category category) {
    Integer articleTypeCode = cmsArticleDO.getArticleTypeCode();
    if (articleTypeCode == null) {
      return null;
    }
    ArticleType articleType = EnumUtils.getByCode(articleTypeCode, ArticleType.class);
    AbstractArticle article = null;
    switch (articleType) {
      case Album:
        article = new Album();
        ((Album) article).addImages(cmsArticleDO.getImages());
        break;
      case Common:
        break;
      default:
        break;
    }
    article.setId(cmsArticleDO.getId());
    article.setTitle(cmsArticleDO.getTitle());
    article.setBrief(cmsArticleDO.getBrief());
    article.setContent(cmsArticleDetailDO.getContent());
    article.setCoverFile(cmsArticleDO.getCoverFile());
    article.setSysInsertTime(cmsArticleDO.getSysInsertTime());
    article.setClickcount(cmsArticleDO.getClickcount());
    article.setTags(tags);
    article.setCommentcount(cmsArticleDO.getCommentcount());
    if (cmsArticleDetailDO != null) {
      article.setContent(cmsArticleDetailDO.getContent());
    }
    article.setCategory(category);
    return article;
  }

  public static CmsArticleDO toCmsArticleDO(Album album) {
    CmsArticleDO cmsArticleDO = new CmsArticleDO();
    cmsArticleDO.setId(album.getId());
    cmsArticleDO.setTitle(album.getTitle());
    cmsArticleDO.setBrief(album.getBrief());
    cmsArticleDO.setCoverFile(album.getCoverFile());
    cmsArticleDO.setSysInsertTime(album.getSysInsertTime());
    cmsArticleDO.setClickcount(album.getClickcount());
    cmsArticleDO.setArticleTypeCode(album.getArticleType().getCode());
    if (album.getTags() != null && album.getTags().size() > 0) {
      List<Long> tagids = new ArrayList<>();
      for (Tag tag : album.getTags()) {
        tagids.add(tag.getId());
      }
      cmsArticleDO.setTagIDs(tagids);
    }
    cmsArticleDO.setCommentcount(album.getCommentcount());
    cmsArticleDO.setCollectSource(album.getCollectSource());
    cmsArticleDO.setCollectSourceMD5(album.getCollectSourceMD5());
    cmsArticleDO.setImages(album.getImages());
    cmsArticleDO.setImagecount(album.getImages() != null ? album.getImages().size() : 0);
    cmsArticleDO.setCategoryId(album.getCategory().getId());
    return cmsArticleDO;
  }
}
